package com.vcredit.creditcat.web.api;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.IAuthorizeManager;
import com.vcredit.creditcat.api.ISMSMessageManager;
import com.vcredit.creditcat.dto.UserTempParamDto;
import com.vcredit.creditcat.dto.UserTempSmsParamDto;
import com.vcredit.creditcat.enumtype.SendSmsTypeEnum;
import com.vcredit.creditcat.exception.UserTempException;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.util.EnumExtUtil;
import com.vcredit.creditcat.vo.JsonResult;
import com.vcredit.framework.annotation.Log;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by liaoshengjun on 2017/7/18.
 */
@RestController
@RequestMapping("/userTemp")
public class userTempController extends BaseCreditCatController{
	@Reference(version = "1.0")
	private IAuthorizeManager authorizeManager;
	@Reference(version = "1.0")
	private ISMSMessageManager SMSmessageManager;
	/**
	 * 渠道用户注册
	 * @param loginParam
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/userTempRegister",method = RequestMethod.POST)
	public JsonResult userTempRegister(@RequestBody @Valid UserTempParamDto loginParam, HttpServletRequest request)
			throws Exception{
		if (!loginParam.getLoginName().matches("^1\\d{10}$")) {
			throw new UserTempException("手机号码格式错误");
		}
		if(!checkVerifyCode(request, loginParam.getLoginName(), loginParam.getSmsVCode(),
				SendSmsTypeEnum.UserTempRegister.getCode())){
			throw new UserTempException("验证码错误");
		}
		//BBS
//		String channel = loginParam.getChannel();
//		if ("BBS".equals(channel)) {
//			authorizeManager.discuzRegister(loginParam);
//		} else {
		authorizeManager.userTempRegister(loginParam);
//		}

		return JsonResult.SuccessResponse(null);
	}

	/**
	 * 渠道用户注册
	 * @param loginParam
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/bbsTempRegister",method = RequestMethod.POST)
	public JsonResult bbsTempRegister(@RequestBody @Valid UserTempParamDto loginParam, HttpServletRequest request)
			throws Exception{
		if (!loginParam.getLoginName().matches("^1\\d{10}$")) {
			throw new UserTempException("手机号码格式错误");
		}
		String signJs = authorizeManager.discuzRegister(loginParam);
		return JsonResult.SuccessResponse(signJs);
	}
	/**
	 * 发送短信验证码
	 * @author liaoshengjun
	 * @param smsParam
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/vercode")
	public JsonResult sendSMS(@RequestBody @Valid UserTempSmsParamDto smsParam, HttpServletRequest request) throws Exception {
		String mobile = smsParam.getMobileNo();
		SendSmsTypeEnum accountType =  EnumExtUtil.getEnumOnValue(SendSmsTypeEnum.class,
				String.valueOf(smsParam.getBusinessType()), "code");
		if(accountType==null){
			throw new UserTempException("未知的消息类型");
		}
		// 4、渠道用户注册
		Integer businessType ;
		switch (accountType) {
			case UserTempRegister:
				businessType = SendSmsTypeEnum.UserTempRegister.getCode();
				break;
			default:
				throw new UserTempException("未知的消息类型");
		}
		authorizeManager.userTempRegisterCheck(smsParam);
		authorizeManager.userTempCheckSupplement(smsParam);
		//注释800万数据撞库逻辑20171027
		//authorizeManager.userTempRegisterReCheck(smsParam);
		String code = createVerifyCode(request, mobile, businessType);
		SMSmessageManager.sendVerificationCode(mobile, code, businessType);
		return JsonResult.SuccessResponse(null);
	}
}
